/*
 * CopyRouteCommand.java
 *
 * Created on October 5, 2007, 3:06 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package drb;

import java.io.BufferedReader;
import java.sql.*;

/**
 *
 * @author Asger
 */
public class CopyRouteCommand extends PromptingCommand {
    
    /** Creates a new instance of CopyRouteCommand */
    public CopyRouteCommand(BufferedReader input) {
        super(input);
    }
    
    public void execute() throws SQLException {
        
        // Get the route to copy
        ScheduleRoute route = promptScheduleRoute("Please enter the starting station of the route you wish to copy:", "Please enter the Id of the route you wish to copy");
        
        // Ask for week days for the new route
        System.out.println("-------------------------------");
        System.out.println("What week days should be new route be active? (Default is " + route.schedules.get(0).weekdays);
        String weekdays = promptWeekDays(route.schedules.get(0).weekdays);
        
        // Ask for new time of departure
        System.out.println("-------------------------------");
        System.out.println("The duration of the route is: " + formattedTimeInterval(route.getDuration()));
        long start = promptTime("When should be train depart on the new route? (Old route departed at " + formattedTime(route.schedules.get(0).departure) +")" );
        
        System.out.println("-------------------------------");
        System.out.println("Creating schedule...");
        
        // Insert a copy of the route. 
        // Place everything inside a transaction so we are sure nobody is trying to use the schedule ID we are getting.
        connection.setAutoCommit(false);
        
        // Get an unused schedule ID
        String sql = "SELECT MAX(schedule_id) FROM schedule";
        ResultSet result = connection.createStatement().executeQuery(sql);
        result.next();
        int id = result.getInt(1) + 1; // We know the table is not empty since we are copying an entry from the table.
        
        // Get the time offset to add to all schedule times
        long timeOffset = start - route.schedules.get(0).departure;
        
        for (TrainSchedule schedule : route.schedules) {
            // Insert a copy of this schedule
            long departure = schedule.departure + timeOffset;
            long arrival = schedule.arrival + timeOffset;
            String to = schedule.to;
            String from = schedule.from;
            int ecoWagons = schedule.ecoWagons;
            int fcWagons = schedule.fcWagons;
            int ecoPrice = schedule.ecoPrice;
            int fcPrice = schedule.fcPrice;
            char wagonType = schedule.bedWagons? 'B':'S';
            
            sql = "INSERT INTO schedule (schedule_id, departure, arrival, from_name, to_name, eco_wagons, fc_wagons, week_days, eco_price, fc_price, wagon_type) VALUES " +
                    "("+id+", "+departure+", "+arrival+", '"+escapeSQL(from)+"', '"+escapeSQL(to)+"', "+ecoWagons+", "+fcWagons+", '"+weekdays+"', "+ecoPrice+", "+fcPrice+", '"+wagonType+"')";
            connection.createStatement().execute(sql);
        }
        
        connection.commit();
        connection.setAutoCommit(true);
        
        System.out.println("New schedule created!");
        System.out.println("-------------------------------");
    }
}













